//
//  OccupationVC.m
//  SelfDriving
//
//  Created by apple on 16/2/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "OccupationVC.h"
#import "BusinessService+User.h"
#import "CityModel.h"
@interface OccupationVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table1;
@property (weak, nonatomic) IBOutlet UITableView *table2;

@end

@implementation OccupationVC{
    NSArray *data1;
    NSArray *data2;
    int index;//选择行
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initdata];
}
-(void)initdata{
    
    [[BusinessService shareInstance]getjoblist:[[NSMutableDictionary alloc]init] success:^(id result) {
        data1 = result;
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        CityModel *model = [data1 firstObject];
        [dic setObject:model.id forKey:@"parentId"];
        [[BusinessService shareInstance]getjoblist:dic success:^(id result) {
            data2 = result;
            [_table1 reloadData];
            [_table2 reloadData];
        } failure:^(id result) {
        }];
    } failure:^(id result) {
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == 1991) {
        return data1.count;
    }
    if (tableView.tag == 1994) {
        return data2.count;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    if (tableView.tag == 1991) {
        CityModel *model = [data1 objectAtIndex:indexPath.row];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, tableView.frame.size.width-30, 40)];
        lable.font = [UIFont systemFontOfSize:15];
        lable.textAlignment = NSTextAlignmentLeft;
        lable.text = model.name;
        lable.textColor = [UIColor grayColor];
        [cell.contentView addSubview:lable];
        
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 20, 20)];
        [img sd_setImageWithURL:[NSURL URLWithString:model.icon]
               placeholderImage:[UIImage imageNamed:@"notimgs"]];
        [cell.contentView addSubview:img];
        if (indexPath.row == index) {
            cell.backgroundColor = UIColorFromRGB(0xf1f1f1);
        }
    }
    if (tableView.tag == 1994) {
        CityModel *model = [data2 objectAtIndex:indexPath.row];
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, tableView.frame.size.width, 45)];
        lable.font = [UIFont systemFontOfSize:15];
        lable.textAlignment = NSTextAlignmentLeft;
        lable.text = model.name;
        [cell.contentView addSubview:lable];
        
        cell.backgroundColor = UIColorFromRGB(0xf1f1f1);
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 1991) {
        index = (int)indexPath.row;
        [_table1 reloadData];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        CityModel *model = [data1 objectAtIndex:indexPath.row];
        [dic setObject:model.id forKey:@"parentId"];
        [[BusinessService shareInstance]getjoblist:dic success:^(id result) {
            data2 = result;
            [_table2 reloadData];
        } failure:^(id result) {
        }];
    }else{
        CityModel *model = [data2 objectAtIndex:indexPath.row];
        [self.delegate didSelectDate:model.name];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (IBAction)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
